<?php

namespace app\api\controller\fruit\tt\v1;

use app\admin\model\TtFruitAchieve;
use think\cache\driver\Redis;

class Warriorweapon extends Warrior
{

    // 获取武器界面相关数据
    public function get_weapon_info()
    {
        $video_num = 0; // 观看视频数
        $invite_num = count(explode(',', $this->user->invite_info)); // 邀请用户数

        $this->handle_weapon_condition($video_num, $invite_num);

        $data['shop_list'] = $this->get_weapon_mall(); // 武器商店数据
        $data['current_weapon'] = $this->user->weapon; // 当前使用的武器
        $data['have_list'] = array_unique(explode(',', $this->user->weapon_list)); // 已拥有武器
        $data['experience_weapon'] = null; // 限时使用武器
        $data['video_num'] = $video_num;
        $data['invite_num'] = $invite_num;

        $this->result('weapon list', $data);
    }

    // 处理
    public function handle_weapon_condition($video_num, $invite_num)
    {
        $weapon_define = $this->get_weapon_define();
        $have_weapon_list = array_unique(explode(',', $this->user->weapon_list)); // 已拥有武器
        foreach ($weapon_define as $weapon) {
            if ($weapon && !in_array($weapon->id, $have_weapon_list)) {
                if ($weapon && $weapon->condition) {
                    $arr = explode('_', $weapon->condition);
                    $type = intval($arr[0]);
                    $con = intval($arr[1]);
                    switch ($type) {
                        case 7:
                            $now_num = $video_num;
                            break;
                        case 13:
                            $now_num = $invite_num;
                            break;
                        default:
                            $now_num = 0;
                            break;
                    }
                    if ($now_num >= $con) {
                        $this->user->weapon_list = $this->user->weapon_list . ',' . $weapon->id;
                        $this->user->save();
                    }
                }
            }
        }
    }

    // 获取看视频次数
    private function get_video_num()
    {
        $video_num = TtFruitAchieve::where('user_id', $this->user->id)->value('video_num');
        if ($video_num == 'get' || $video_num == 'complete') {
            return 5;
        } elseif ($video_num >= 5) {
            return 5;
        } else {
            return intval($video_num);
        }
    }

    // 购买武器
    public function buy_weapon()
    {
        $id = $this->request->post('id');

        $weapon_define = $this->get_weapon_define();
        $have_weapon_list = array_unique(explode(',', $this->user->weapon_list));

        if (!in_array($id, $have_weapon_list)) {
            $weapon = $weapon_define->$id;
            if ($weapon && $weapon->id == $id && $weapon->condition && $weapon->shop == 1) {
                if ($this->handle_cost($weapon->condition)) {
                    $this->user->weapon_list = $this->user->weapon_list . ',' . $id;
                    if ($this->user->save() !== false) {
                        // 处理武器成就
                        $this->handle_weapon_achieve($weapon_define, $have_weapon_list);
                        $data['coin'] = $this->user->coin;
                        $data['diamond'] = $this->user->diamond;
                        $data['weapon'] = $id;
                        $this->result('ok', $data);
                    }
                }
            }
        }
        $this->error('fail');
    }

    // 处理武器成就
    public function handle_weapon_achieve($weapon_define, $have_weapon_list)
    {
        $weapon_1 = 0;
        $weapon_2 = 0;
        foreach ($weapon_define as $weapon) {
            if ($weapon) {
                if ($weapon->type == 1 && in_array($weapon->id, $have_weapon_list)) {
                    $weapon_1++;
                }
                if ($weapon->type == 2 && in_array($weapon->id, $have_weapon_list)) {
                    $weapon_2++;
                }
            }
        }
        $achieve = TtFruitAchieve::get(['user_id' => $this->user->id]);
        $achieve->weapon_1 = $weapon_1;
        $achieve->weapon_2 = $weapon_2;
        $achieve->save();
    }

    // 切换武器
    public function change_weapon()
    {
        $weapon_id = $this->request->post('weapon_id'); // 需要切换的武器id
        $have_weapon_list = explode(',', $this->user->weapon_list); // 已拥有的武器list

        if (in_array($weapon_id, $have_weapon_list)) { // 拥有这个武器id
            $this->user->weapon = $weapon_id;
            if ($this->user->save() !== false) {
                $data['weapon'] = $this->user->weapon;
                $this->result('ok', $data);
            }
        }
        $this->error('fail');
    }

    // 获取幸运武器
    public function get_lucky_weapon()
    {
        $id = $this->request->post('id');

        $weapon_define = $this->get_weapon_define();
        $have_weapon_list = array_unique(explode(',', $this->user->weapon_list));

        if (!in_array($id, $have_weapon_list)) {
            $weapon = $weapon_define->$id;
            if ($weapon && $weapon->id == $id && $weapon->shop == 2) {
                $this->user->weapon = $id;
                $this->user->weapon_list = $this->user->weapon_list . ',' . $id;
                if ($this->user->save() !== false) {
                    $item_id = $this->get_item_by_weapon($id);
                    $this->result('ok', 'a_' . $item_id . '_1');
                }
            }
        }
        $this->error('fail');
    }

    // 试用武器
    public function experience_weapon()
    {
        $redis = new Redis();
        $experience = $redis->get($this->user->openid . '_tt_fruit_exp_weapon');
        // 查询是否已经有试用武器
        if (!$experience) {
            // 六分之一的概率
            $rand = mt_rand(1, 6);
            if ($rand == 6) {
                $weapon_define = $this->get_weapon_define();
                $have_weapon_list = array_unique(explode(',', $this->user->weapon_list));
                $not_have_exp = []; // 未拥有的可试用武器
                foreach ($weapon_define as $weapon) {
                    if ($weapon->experience == 1) {
                        if (!in_array($weapon->id, $have_weapon_list)) {
                            array_push($not_have_exp, $weapon->id);
                        }
                    }
                }
                if (count($not_have_exp) > 0) {
                    $this->result('ok', $not_have_exp[array_rand($not_have_exp)]);
                }
            }
        }
        $this->result('fail');
    }

    // 获取试用武器
    public function get_experience_weapon()
    {
        $id = $this->request->post('id');

        $redis = new Redis();
        $experience = $redis->get($this->user->openid . '_tt_fruit_exp_weapon');

        if (!$experience) {
            $have_weapon_list = array_unique(explode(',', $this->user->weapon_list));
            if (!in_array($id, $have_weapon_list)) {
                $redis->set($this->user->openid . '_tt_fruit_exp_weapon', $id, 86400);
                $redis->set($this->user->openid . '_tt_fruit_use_exp_weapon', 1, 86400);
                $item_id = $this->get_item_by_weapon($id);
                $this->result('ok', 'a_' . $item_id . '_1');
            }
        }
        $this->result('fail');
    }

}
